Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add whereNot() and orWhereNot() to query builders #36522

Closed
wants to merge 1 commit into from

Conversation

shadoWalker89
Copy link
Contributor

This PR adds two new methods whereNot() and orWhereNot() to the Query builder and the Eloquent Query builder
This PR comes with tests

Usage example

// whereNot
$query->select('*')->from('users')->whereNot('name', "foo")->whereNot('name', '<>', "bar")

'select * from "users" where not "name" = ? and not "name" <> ?'

// orWhereNot
$query->select('*')->from('users')->orWhereNot('name', "foo")->orWhereNot('name', '<>', "bar")

'select * from "users" where not "name" = ? or not "name" <> ?'

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@shadoWalker89
Copy link
Contributor Author

@taylorotwell Hi 😄
All other methods like have the Not variant

whereIn() and whereNotIn()
orWhereIn() and orWhereNotIn()
whereNull() and whereNotNull()
orWhereNull() and orWhereNotNull()
whereExists() and whereNotExists()
orWhereExists() and orWhereNotExists()
whereBetween() and whereNotBetween()
orWhereBetween() and orWhereNotBetween()

So it's only logical to add the simple Not variant to where() and orWhere()
If you could give this a thought that would be cool

@lk77
Copy link

lk77 commented Mar 9, 2021

I agree, it would be cleaner.

doing :

->where('field', '!=', 'someValue');

all the time. whereNot('field', 'someValue') would be more readable

it's more of a syntactic sugar than anything

@shadoWalker89
Copy link
Contributor Author

@lk77
It's not a syntactic sugar it's actually a valid sql thing, in sql you can do a not
https://dev.mysql.com/doc/refman/8.0/en/logical-operators.html

I'm actually using this for negating a group of wheres

where not ( condition and condition and condition and condition )

@sebastianvitterso
Copy link

@taylorotwell Is there any chance for this to get reconsidered? We've run into use-cases for this multiple times since we first saw this a month ago, and I'm sure we're not alone about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants